Page({
  data: {
    drafts: [] 
  },

  onLoad() {
    this.getDrafts();
  },

  getDrafts() {
    wx.request({
      url: '', 
      method: 'GET',
      success: (res) => {
        if (res.statusCode === 200) {
          const sortedDrafts = res.data.sort((a, b) => new Date(b.time) - new Date(a.time));
          this.setData({
            drafts: sortedDrafts
          });
        } else {
          wx.showToast({
            title: '获取草稿列表失败',
            icon: 'none'
          });
        }
      },
      fail: (err) => {
        console.error('获取草稿列表失败：', err);
        wx.showToast({
          title: '网络错误',
          icon: 'none'
        });
      }
    });
  },

  viewDraft(e) {
    const draftId = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: `/pages/paper/paper?id=${draftId}`
    });
  }
});
